package Json;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ajie
 * @date 2022/5/13
 * @function
 */
public class JsonListChild {
    public static void main(String[] args) {
        MenuData menuData = new MenuData();
        menuData.index =1;
        menuData.name = "name1";
        menuData.delateAble = false;
        menuData.isWork = true;
        menuData.recordTAG = 11;

        MenuData menuData2 = new MenuData();
        menuData2.index =1;
        menuData2.name = "name1Child";
        menuData2.delateAble = false;
        menuData2.isWork = true;
        menuData2.recordTAG = 1001;

        ArrayList<MenuData> list = new ArrayList<>();
        list.add(menuData2);
        menuData.childMenu = list;

        ArrayList<MenuData> origin = new ArrayList<>();
        boolean add = origin.add(menuData);

        String s = new Gson().toJson(origin);
        System.out.println(s);

        List<MenuData> resultList = new Gson().fromJson(s, new TypeToken<List<MenuData>>() {}.getType());

        String name = resultList.get(0).childMenu.get(0).name;
        System.out.println("name："+name);

        List<MenuData> resultList2 = new Gson().fromJson(s, new TypeToken<List<MenuData2>>() {}.getType());
        String name2 = resultList2.get(0).childMenu.get(0).name;
        System.out.println("name2："+name2);


    }

    static class MenuData {//菜单
        int index;
        String name;//第三方的 tag 标记
        boolean delateAble;//能否删除，原始的菜单，不能删除。
        boolean isWork;//是否打开开关。
        int recordTAG;//
        List<MenuData> childMenu;//子菜单。
    }


}
